<?php

/**
 * Pager.cls.php
 */
class Pager {

    /**
     * url
     * @access private
     * @var string
     */
    var $url = null;

    /**
     * 条目偏移量
     * @access private
     * @var integer
     */
    var $offset = 0;

    /**
     * 条目总数
     * @access private
     * @var integer
     */
    var $itemSum;

    /**
     * 每页的条目数
     * @access private
     * @var integer
     */
    var $pageSize;

    /**
     * 总页数
     * @access private
     * @var integer
     */
    var $pageCount;

    /**
     * 当前页号码
     * @access private
     * @var integer
     */
    var $currentPageNumber;

    /**
     * url中的页码标志符
     * @access private
     * @var string
     */
    var $flag = 'p';

    /**
     * separator : url中的串联记号 (?|&)
     * @access private
     * @var string
     */
    var $separator = '?';

    /**
     * 翻页栏中显示页号的数量
     * @access private
     * @var integer
     */
    var $barNumberCount = 10;

    /**
     * 分页栏页面总数
     */
    var $barPageCount;

    /**
     * 当前的分页栏号
     */
    var $barCurrentPage;

    /**
     * Constructor
     * @access public
     * @param string $url
     * @param integer $itemSum 条目总数
     * @param integer $pageSize 每页的条目数
     * @param integer $currentPageNumber 当前页号码
     * @param string $flag 放置在url中的翻页标识
     */
    function Pager($url, $itemSum, $pageSize = 10, $currentPageNumber = 1, $flag) {
        $this->flag = $flag;
        if (preg_match('/\?/', $url))
            $this->separator = '&';
        $this->url = $url;
        $this->currentPageNumber = max(1, $currentPageNumber);
        $this->itemSum = $itemSum;
        $this->pageSize = $pageSize;
        $this->pageCount = ceil($itemSum / $pageSize);
        if ($this->currentPageNumber > $this->pageCount) {
            $this->currentPageNumber = $this->pageCount;
        }
        $this->offset = $pageSize * ($this->currentPageNumber - 1);

        // 分页栏的总页数 : ceil($this->pageCount/$this->barNumberCount)
        // 分页栏的当前页数 ： ceil($this->currentPageNumber/$this->barNumberCount)
        $this->barPageCount = ceil($this->pageCount / $this->barNumberCount);
        $this->barCurrentPage = ceil($this->currentPageNumber / $this->barNumberCount);
    }

    /**
     * 返回每页的条目数量
     * @access public
     * @return integer
     */
    function getPageSize() {
        return $this->pageSize;
    }

    /**
     * 返回条目的偏移量
     * @access public
     * @return integer
     */
    function getOffset() {
        return $this->offset;
    }

    /**
     * 取翻页栏的html页面代码
     * @access public
     * @return string
     */
    function getBar() {
        $template = '<ul><li >共有记录数:0</li></ul>';
        if ($this->itemSum == 0)
            return $template;

        $first = '&lt;&lt;';
        $last = '&gt;&gt;';
        $preceding = '上一页';
        $following = '下一页';
        $following_ten = '下10页';
        $preceding_ten = '上10页';

        $numberLine = ''; // 页码数字链接
        // first 
        // 如果当前页超过 $barNumberCount , 这个链接生效
        if ($this->currentPageNumber > $this->barNumberCount) {
            $first = '<li ><a href="' . $this->url . $this->separator . $this->flag .
                    '=1">' . $first . '</a></li>';
        } else {
            $first = '<li class="disabled"><a>' . $first . '</a></li>';
        }

        //preceding_ten
        if ($this->currentPageNumber - 10 > 0) {
            $preceding_ten = '<li ><a href="' . $this->url . $this->separator . $this->flag .
                    '=' . ($this->currentPageNumber - 10) . '">' . $preceding_ten . '</a></li>';
        } else {
            $preceding_ten = '<li class="disabled"><a>' . $preceding_ten . '</a></li>';
        }

        //post_ten
        if ($this->currentPageNumber + 10 <= $this->pageCount) {
            $following_ten = '<li ><a href="' . $this->url . $this->separator . $this->flag .
                    '=' . ($this->currentPageNumber + 10) . '">' . $following_ten . '</a></li>';
        } else {
            $following_ten = '<li class="disabled"><a>' . $following_ten . '</a></li>';
        }

        // last
        if ($this->barCurrentPage < $this->barPageCount) {
            $last = '<li ><a href="' . $this->url . $this->separator . $this->flag .
                    '=' . $this->pageCount . '">' . $last . '</a></li>';
        } else {
            $last = '<li class="disabled"><a>' . $last . '</a></li>';
        }
        // preceding
        // 如果当前页不等于1，这个链接生效
        if ($this->currentPageNumber > 1) {
            $preceding = '<li ><a href="' . $this->url . $this->separator . $this->flag .
                    '=' . ($this->currentPageNumber - 1) . '">' . $preceding . '</a></li>';
        } else {
            $preceding = '<li class="disabled"><a>' . $preceding . '</a></li>';
        }
        // following
        // 如果当前页不是最后一页，这个链接生效
        if ($this->currentPageNumber < $this->pageCount) {
            $following = '<li ><a href="' . $this->url . $this->separator . $this->flag .
                    '=' . ($this->currentPageNumber + 1) . '">' . $following . '</a></li>';
        } else {
            $following = '<li class="disabled"><a>' . $following . '</a></li>';
        }
        
        //////////////////////////////
        // page numbers	    
        $startNumber = ($this->currentPageNumber < 6) ? 1 : $this->currentPageNumber - 5;
        $limitNumber = ($this->currentPageNumber + 6 > $this->pageCount) ? $this->pageCount : $this->currentPageNumber + 6;
        for ($i = $startNumber; $i <= $limitNumber; $i++) {
            if ($i != $this->currentPageNumber) {
                $numberLine .= '<li><a href="' . $this->url . $this->separator . $this->flag .
                        '=' . $i . '">' . $i . '</a></li>';
            } else {
                $numberLine .= '<li class="active"><a>' . $i . '</a></li>';
            }
        }

        // 模板代码
        $template = '<ul>' . $first . $preceding_ten . $preceding . $numberLine . $following . $following_ten . $last . '</ul>';
        return $template;
    }

}

// ------------------- test -------------- //
//header("Content-Type: text/html;charset=utf-8");
//$page = new Pager('http://zhong/Test/int.php',108,10,$_GET['p']);
//print $page->getBar();
?>